const util = require('../../utils/util.js')

import { judge } from '../publicity/judge.js'
import { client } from '../../request/index.js'

Page({
  data: {
    ip: getApp().globalData.ip,
    imgIp: getApp().globalData.imgIp,
    author: '',
    content: '',
    imgBox: [],
    uri: [],
    title: '',
    titleFontNum: 0,
    size: { maxHeight: 500, minHeight: 100 },
  },

  async upLoadInfor() {
    const time = util.formatTime(new Date())
    const that = this
    const cookie = wx.getStorageSync('sessionid')
    this.setData({
      createtime: time,
    })
    const getData = await client({
      url: '/advertisement/advertisementinfo',
      data: {
        author: that.data.author,
        content: that.data.content,
        coverPicUrl: that.data.uri.join(' '),
        createtime: time,
        title: that.data.title,
      },
      method: 'POST',
    })
    console.log('公益宣传', getData)
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000,
    })
    this.setData({
      author: '',
      content: '',
      title: '',
      imgBox: [],
      uri: [],
    })
    that.onLoad()
  },

  onBtn(e, index = 0) {
    let that = this
    const { author, content, title } = that.data
    const judgement = judge(author, content, title)
    if (that.data.imgBox.length !== 0 && judgement === true) {
      console.log('正在上传：' + that.data.imgBox[index])
      wx.uploadFile({
        url: getApp().globalData.ip + '/upload',
        filePath: that.data.imgBox[index], // 图片文件路径
        name: 'file',
        header: {
          cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
          'content-type': 'charset=utf-8',
        },
        dataType: 'json',
        success: function (res) {
          let datas = JSON.parse(res.data)
          console.log('imgBox[' + index + ']上传成功！')
          let new_url = that.data.uri.concat(datas.data)
          that.setData({
            uri: new_url,
          })
          if (index == that.data.imgBox.length - 1) {
            that.upLoadInfor(e.detail.value)
          } else that.onBtn(e, index + 1)
        },
      })
      that.setData({
        titleFontNum: 0,
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请填写完整信息',
        confirmColor: '#ff6b69',
      })
    }
  },

  changeTitle: function (e) {
    let inputSearch = e.detail.value
    this.setData({
      title: inputSearch,
    })
  },

  changeAuthor: function (e) {
    let inputSearch = e.detail.value
    this.setData({
      author: inputSearch,
    })
  },

  changeContent: function (e) {
    let inputSearch = e.detail.value
    this.setData({
      content: inputSearch,
    })
  },
  // 选择
  chooseImage: function () {
    let that = this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: '#000000',
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.addImg('album')
          } else if (res.tapIndex == 1) {
            that.addImg('camera')
          }
        }
      },
    })
  },
  // 添加图片
  addImg(type) {
    let that = this
    let imgBox = that.data.imgBox
    wx.chooseImage({
      count: 3,
      sizeType: ['compressed'],
      sourceType: [type],
      success(res) {
        let tempFilePaths = res.tempFilePaths
        imgBox = imgBox.concat(tempFilePaths)
        that.setData({
          imgBox: imgBox,
        })
      },
    })
  },
  // 查看图片
  viewImg(e) {
    console.log(e)
    let imgUrl = e.currentTarget.dataset.url
    wx.previewImage({
      urls: [imgUrl],
      current: '',
      success: function () {},
    })
  },
  //删除图片
  imgDelete: function (e) {
    let that = this
    let index = e.currentTarget.dataset.deindex
    let imgBox = this.data.imgBox
    imgBox.splice(index, 1)
    that.setData({
      imgBox: imgBox,
    })
  },
})
